package cn.likole.oj.service;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.random.R;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.net.URL;

@IocBean
public class EmailService {

    private static final Log log= Logs.get();

    @Inject("refer:$ioc")
    protected Ioc ioc;

    public boolean send(String to,String subject,String html){
        try {
            HtmlEmail htmlEmail=ioc.get(HtmlEmail.class);
            htmlEmail.setSubject(subject);
            htmlEmail.setHtmlMsg(html);
            htmlEmail.addTo(to);
            htmlEmail.buildMimeMessage();
            htmlEmail.sendMimeMessage();
            return true;
        } catch (Throwable e) {
            log.info("send email faild",e);
            return false;
        }
    }
}
